let currentPage = 0;
const totalPages = 10; // 包括封面和RSVP页面
let autoPlayTimer;
let touchstartY = 0;
let touchendY = 0;
const threshold = window.innerHeight * 0.02;
let autoPlay = false;

function handleGesture() {
  distance = Math.abs(touchendY - touchstartY)
  console.log(distance)
  if (distance > threshold) {
    if (currentPage != 0) {
      clearTimeout(autoPlayTimer)
      if (touchendY < touchstartY) {
        toPage(currentPage + 1); // 向上滑动，显示下一页
      } else {
        toPage(currentPage - 1); // 向上滑动，显示下一页
      }
    }
  }

}

document.addEventListener("touchstart", (e) => {
  touchstartY = e.changedTouches[0].screenY;
}, false);

document.addEventListener("touchend", (e) => {
  touchendY = e.changedTouches[0].screenY;
  handleGesture();
}, false);

function nextPage(pageNumber) {
  clearTimeout(autoPlayTimer); // 清除之前的计时器
  if (!autoPlay) {
    return
  }

  var currentPageDiv = document.getElementById("page" + currentPage);
  var nextPageDiv = document.getElementById("page" + pageNumber);

  if (currentPageDiv && currentPage != 0) {
    currentPageDiv.addEventListener("animationend", function () {
      this.classList.add("fade-out");
      this.style.display = "none";
      this.classList.remove("fade-out");
    }, {once: true});
  }

  currentPage = pageNumber;
  if (currentPage == 2) {
    sweetLove();
  }

  if (nextPageDiv) {
    var list = [
      "block",
      "block",
      "block",
      "block",
      "flex",
      "flex",
      "block",
      "flex",
      "flex",
      "block",
      "flex",
      "flex",
    ];
    nextPageDiv.classList.add("fade-in");
    nextPageDiv.style.display = list[pageNumber + 1];
  }

  if (currentPage < totalPages) {
    var list = [
      5000,
      5000,
      5000,
      5000,
      5000,
      5000,
      5000,
      5000,
      5000,
      5000,
      5000,
    ];
    autoPlayTimer = setTimeout(function () {
      nextPage(currentPage + 1);
    }, list[currentPage - 1]); // 自动播放到下一页
  }
}

function startAutoPlay() {
  autoPlay = true;
  var music = document.getElementById("background-music");
  var playIcon = document.querySelector(".fa-play");
  var pauseIcon = document.querySelector(".fa-music");

  if (music.paused) {
    music.play();
    playIcon.style.display = "none";
    pauseIcon.style.display = "inline-block";
    pauseIcon.classList.add("rotating");
  }

  var musicBtn = document.getElementById("music-btn");
  musicBtn.style.display = "inline-block"
  var e = document.getElementById("page0");
  e.classList.add("fade-out");
  e.addEventListener("animationend", function () {
    this.style.display = "none";
    this.classList.remove("fade-out");
    nextPage(1); // 开始自动播放从第1页开始
  }, {once: true});

}

function submitRSVP() {
  var name = document.getElementById("name").value;
  if (name) {
    alert("Thank you for your response, " + name + "!");
  } else {
    alert("Please enter your name.");
  }
}

function toggleMusic() {
  var music = document.getElementById("background-music");
  var playIcon = document.querySelector(".fa-play");
  var pauseIcon = document.querySelector(".fa-music");

  if (music.paused) {
    music.play();
    playIcon.style.display = "none";
    pauseIcon.style.display = "inline-block";
    pauseIcon.classList.add("rotating");
  } else {
    music.pause();
    playIcon.style.display = "inline-block";
    pauseIcon.style.display = "none";
    pauseIcon.classList.remove("rotating");
  }
}

function sweetLove() {
  // 添加动画类
  // setTimeout(function () {
  //   var element = document.getElementById("sweet-love");
  //   element.classList.add("bounce");
  // }, 1000); // 调整为你需要的时间
}

window.onload = function () {

}


function toPage(pageNumber) {
  console.log(pageNumber)
  autoPlay = false;
  if (pageNumber > totalPages) {
    return
  }
  if (pageNumber < 0) {
    return
  }
  var currentPageDiv = document.getElementById("page" + currentPage);
  var nextPageDiv = document.getElementById("page" + pageNumber);
  currentPageDiv.style.display = 'none';
  currentPage = pageNumber
  if (nextPageDiv) {
    console.log(nextPageDiv)
    var list = [
      "block",
      "block",
      "block",
      "block",
      "flex",
      "flex",
      "block",
      "flex",
      "flex",
      "block",
      "flex",
      "flex",
    ];
    nextPageDiv.style.display = list[pageNumber + 1];
    console.log(pageNumber + 1);
    console.log(nextPageDiv);
    console.log(currentPageDiv);
  }

}
